/*
 * Copyright (c) 2024. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package org.example.consumer;

import com.google.protobuf.InvalidProtocolBufferException;
import com.googlecode.protobuf.format.JsonFormat;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.example.HelloDubbo;
import org.example.consumer.service.HelloWorldService;
import org.example.provider.triple.GenericMessage;
import org.example.provider.triple.GreeterReply;
import org.example.provider.triple.GreeterRequest;
import org.example.provider.triple.GreeterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = "org.example.consumer")
@EnableDubbo
public class Dubbo3ConsumerApplication implements CommandLineRunner {

    // Simple example shows how a command line spring application can execute an
    // injected bean service. Also demonstrates how you can use @Value to inject
    // command line args ('--name=whatever') or application properties

    @Autowired(required = false)
    private HelloWorldService helloWorldService;

//    @DubboReference(registry = "zk-registry")
  /*  @DubboReference
    private HelloDubbo helloDubbo;*/

    //    @DubboReference(registry = "zk-registry", proxy = CommonConstants.NATIVE_STUB,group = "group1",version = "1.0")
  /*  @DubboReference(timeout = 10000, url = "dubbo://127.0.0.1:20881", proxy = CommonConstants.NATIVE_STUB, group = "group1", version = "1.0")
    private GreeterService greeter;*/

    @Override
    public void run(String... args) {
        System.out.println(this.helloWorldService.getHelloMessage());
//        System.out.println(this.helloDubbo.getHelloMessage());
//        String xieqing = greeter.greetOneToOne(Mono.just(GreeterRequest.newBuilder().setName("xieqing client").build())).block().getMessage();
//        System.out.println(xieqing);

// 		ManyToMany
//		greeter.greetManyToMany(Flux.range(1, 10)
//						.map(num ->
//								GreeterRequest.newBuilder().setName(String.valueOf(num)).build())
//						.doOnNext(req -> System.out.println("consumeManyToMany request data: "+req)))
//				.subscribe(res -> System.out.println("consumeManyToMany get response: "+res));


       /* greeter.anyDemo(Mono.just(GreeterRequest.newBuilder().setName("xieqing client").build()))
                .subscribe(res -> {
                    try {
                        if (res.getValue() == null) {
                            System.out.println("consumeAnyDemo get response: null");
                            return;
                        }
                        System.out.println("consumeAnyDemo get response: " + new JsonFormat().printToString(res));
                    } catch (Throwable e) {
                        throw new RuntimeException(e);
                    }
                });*/
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Dubbo3ConsumerApplication.class, args);
    }
}
